package com.fmsz.xmly.adapters.classify.tag;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.fmsz.xmly.R;
import com.fmsz.xmly.entitys.classify.tag.ClassifyTagEntity;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/12.
 */

public class ClassifyTagHotAdapter extends RecyclerView.Adapter<ClassifyTagHotAdapter.ViewHolder>{

    private Context context;
    private List<ClassifyTagEntity.FocusImagesEntity.FocusImagesList> images;
    private List<ClassifyTagEntity.CategoryContentsEntity.CategoryContentsList> datas;
    private RecyclerView recycleView;
    private List<ClassifyTagEntity.TagEntity.TagListEntity> tags;
    private ClassifyTagEntity entity;

    public ClassifyTagHotAdapter(Context context) {
        this.context = context;
        datas = new ArrayList<>();
        images = new ArrayList<>();
        tags = new ArrayList<>();
    }

    @Override
    public int getItemViewType(int position) {
        if (position==0) {
            return R.layout.classify_tag_header;
        }else if(position==1){
            return R.layout.classify_tag_hot;
        }
        return R.layout.classify_tag_lv_item;
    }
    public void addAll(ClassifyTagEntity entity){
        images.addAll(entity.getFocusImages().getList());
        datas.addAll(entity.getCategoryContents().getList());
        tags.addAll(entity.getTags().getList());
        this.entity = entity;
        notifyDataSetChanged();
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        if (this.recycleView!=null) {
            return;
        }
        this.recycleView = recyclerView;
        RecyclerView.ItemDecoration decoration = new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                if (parent.getChildAdapterPosition(view)!=0) {
                    outRect.set(0,25,0,0);
                }
            }

            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDrawOver(c, parent, state);
            }
        };
        this.recycleView.addItemDecoration(decoration);
    }


    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recycleView=null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(viewType,parent,false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (getItemViewType(position)== R.layout.classify_tag_header) {
            ImageView[] pagerDatas = new ImageView[images.size()];
            for (int i = 0; i < images.size(); i++) {
                ImageView imageView = new ImageView(context);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                Picasso.with(context).load(images.get(i).getPic()).into(imageView);
                pagerDatas[i] = imageView;
            }
            TagHotPagerAdapter pagerAdapter = new TagHotPagerAdapter(pagerDatas);
            holder.pager.setAdapter(pagerAdapter);
        }else if(getItemViewType(position)==R.layout.classify_tag_hot){
            holder.hot_tv.setText(datas.get(position-1).getTitle());
            holder.subTitle_hot.setText(datas.get(position-1).getList().get(position-1).getSubtitle());
            Picasso.with(context).load(datas.get(position-1).getList().get(position-1).getCoverPath()).into(holder.hot_iv);
        }else if(getItemViewType(position)==R.layout.classify_tag_lv_item){
            if (datas.get(position-1).getList().size()==0) {
                holder.title_bar_tag.setVisibility(View.GONE);
                return;
            }
//            if (position==datas.size()+1) {
//                holder.title_lv.setText(entity.getTags().getTitle());
//                holder.tag_rv.setLayoutManager(new GridLayoutManager(context,3));
//                TagRvItemAdapter itemAdapter = new TagRvItemAdapter(context, datas.get(position-1).getList(), TagRvItemAdapter.TAG_SIMPLE);
//                itemAdapter.addAll(tags);
//                holder.tag_rv.setAdapter(itemAdapter);
//                return;
//            }
                holder.title_lv.setText(datas.get(position-1).getTitle());
                holder.tag_rv.setLayoutManager(new LinearLayoutManager(context));
                TagRvItemAdapter rvItemAdapter = new TagRvItemAdapter(context, datas.get(position - 1).getList());
                holder.tag_rv.setAdapter(rvItemAdapter);
        }
    }

    @Override
    public int getItemCount() {
        return images.size()==0?datas.size():datas.size()+1;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final ViewPager pager;
        private final ImageView hot_iv;
        private final TextView hot_tv;
        private final TextView subTitle_hot;
        private final TextView title_lv;
        private final RecyclerView tag_rv;
        private final LinearLayout title_bar_tag;

        public ViewHolder(View itemView) {
            super(itemView);
            pager = ((ViewPager) itemView.findViewById(R.id.pager_classify_tag));
            hot_iv = ((ImageView) itemView.findViewById(R.id.hot_classify_tag_iv));
            hot_tv = ((TextView) itemView.findViewById(R.id.title_classify_tag_hot));
            subTitle_hot = ((TextView) itemView.findViewById(R.id.subTitle_classify_tag_hot));
            title_lv = ((TextView) itemView.findViewById(R.id.title_tag_lv));
            tag_rv = ((RecyclerView) itemView.findViewById(R.id.tag_rv));
            title_bar_tag = ((LinearLayout) itemView.findViewById(R.id.title_bar_tag));
        }
    }
}
